package com.martin.dms.dataservices.hib;

import java.util.List;

import org.hibernate.Session;

import com.martin.dms.dataservices.ItemDataService;
import com.martin.dms.model.Item;
import com.martin.dms.model.hib.CategoryHib;
import com.martin.dms.model.hib.ItemHib;

/**
 * Hibernate implementation of the Item data service.
 * 
 * @author mtsvetkov.
 */
public class ItemDataServiceHib implements ItemDataService {

    private static final String ITEM_GET_ALL_QUERY = "Item.getAll";

    private Session session;

    /**
     * Constructor that expects a hibernate session.
     * 
     * @param session - the current hibernate session.
     */
    public ItemDataServiceHib(Session session) {
        this.session = session;
    }

    /**
     * {@inheritDoc}
     */
    public Item create(Item dmsObject) {
        ItemHib item = new ItemHib();
        item.setName(dmsObject.getName());
        item.setPrice(dmsObject.getPrice());
        item.setDescription(dmsObject.getDescription());
        item.setImage(dmsObject.getImage());
        item.setCategory(dmsObject.getCategory());
        session.save(item);
        return item;
    }

    /**
     * {@inheritDoc}
     */
    public void delete(long id) {
        ItemHib item = get(id);
        session.delete(item);
    }

    /**
     * {@inheritDoc}
     */
    public ItemHib get(long id) {
        return (ItemHib) session.get(ItemHib.class, Long.valueOf(id));
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<Item> getAll() {
        return session.getNamedQuery(ITEM_GET_ALL_QUERY).list();
    }

    /**
     * {@inheritDoc}
     */
    public void addAmount(long itemId, int toAdd) {
        ItemHib item = get(itemId);
        int amount = item.getAmount() + toAdd;
        item.setAmount(amount);
    }

    /**
     * {@inheritDoc}
     */
    public void removeAmount(long itemId, int toRemove) {
        ItemHib item = get(itemId);
        int amount = item.getAmount() - toRemove;
        item.setAmount(amount);
    }

    /**
     * {@inheritDoc}
     */
    public void updateAmount(long itemId, int amount) {
        ItemHib item = get(itemId);
        item.setAmount(amount);
    }

    /**
     * {@inheritDoc}
     */
    public void updateName(long itemId, String name) {
        ItemHib item = get(itemId);
        item.setName(name);
    }

    /**
     * {@inheritDoc}
     */
    public void updateCategory(long itemId, long categoryId) {
        ItemHib item = get(itemId);
        CategoryHib category = (CategoryHib) session.get(CategoryHib.class, Long.valueOf(categoryId));
        item.setCategory(category);
    }

    /**
     * {@inheritDoc}
     */
    public void updateImage(long itemId, byte[] image) {
        ItemHib item = get(itemId);
        if (image == null || image.length == 0) {
            item.setImage(null);
        } else {
            item.setImage(image);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void updateDescription(long itemId, String description) {
        ItemHib item = get(itemId);
        item.setDescription(description);
    }

    /**
     * {@inheritDoc}
     */
    public void updatePrice(long itemId, int price) {
        ItemHib item = get(itemId);
        item.setPrice(price);
    }

}
